package com.gehj.commonui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import java.util.ArrayList;
import java.util.List;

//TODO 自定义dialog
// 1,带透明站位的
// 2,总结bussiness的avloading
// 3,XUI中拓展处理 看有啥好的弹框

/**
 *
 * 各种dialog封装
 * https://www.oschina.net/question/54100_32486
 * @author gehj
 * @date 2021/1/5 10:14
 */

public class DialogUtils {
    private  static int mChoose = -1; //单选需要变量
    private  static List<Integer> mChoices = new ArrayList<>();
    private ProgressDialog progDialog;

    /**
     * 普通的dialog
     * @param context
     * @param content 需要传入的内容
     */
    public  static  void  showNormalDialog(Context context,
                                           String content,
                                           int ResId
                                                ){
        final AlertDialog.Builder builder =
                new AlertDialog.Builder(context);

        builder.setIcon(ResId);//fixme 可替换
        builder.setTitle("提示");
        builder.setMessage(content);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); //fixme
            }
        });

        builder.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        AlertDialog normalDialog = builder.create();
        normalDialog.show(); //默认点击空白消失
        //按对话框以外的地方不起作用。按返回键还起作用
       // normalDialog.setCanceledOnTouchOutside(false);
        //按对话框以外的地方不起作用。按返回键还起作用
        //normalDialog.setCancelable(false);
    }


    /**
     * 列表Dialog
     * @param context
     * @param listItems list的内容
     * @param ResId
     */
    public  static  void showListDialog(Context context,
                                        String[] listItems,
                                        int ResId){
        AlertDialog.Builder listDialogBuilder =
                new AlertDialog.Builder(context);

        listDialogBuilder.setTitle("提示");

        listDialogBuilder.setIcon(ResId);//fixme 可替换

        listDialogBuilder.setItems(listItems, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context,
                        "你点击了" + listItems[which],  // which 下标从0开始
                        Toast.LENGTH_SHORT).show();
            }
        });
        listDialogBuilder.show();
    }


    /**
     * 单选dialog
     * @param context
     * @param listItems
     * @param ResId
     */
    public  static  void showSingleChoiceDialog(Context context,
                                                String[] listItems,
                                                int ResId
                                                ){
        AlertDialog.Builder singleChoiceDialogBuilder =
                new AlertDialog.Builder(context);

        singleChoiceDialogBuilder.setTitle("提示");

        //参数非常多，可以根据需求进行修改
        singleChoiceDialogBuilder.setSingleChoiceItems(listItems,
                        0,//第二个参数是默认选项，此处设置为0
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mChoose = which;
            }
        });

        singleChoiceDialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if ( mChoose != -1) {
                    Toast.makeText(context,
                            "你选择了" + listItems[mChoose],
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        singleChoiceDialogBuilder.show();

    }


    /**
     * 多选dialog
     * @param context
     * @param listItems
     * @param ResId
     */
    public  static  void showMultiChoiceDialog(Context context,
                                               String[] listItems,
                                               int ResId) {
        final boolean initChoiceSets[]={false,false,false,false};// FIXME: 2021/2/26  需要重构

        AlertDialog.Builder multiChoiceDialog =
                new AlertDialog.Builder(context);
        multiChoiceDialog.setTitle("提示");
        multiChoiceDialog.setIcon(ResId);//fixme 可替换
        multiChoiceDialog.setMultiChoiceItems(listItems, initChoiceSets, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked) {
                    mChoices.add(which);
                } else {
                    mChoices.remove(which);
                }
            }
        });

        multiChoiceDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int size = mChoices.size();
                String str = "";
                for (int index:
                     mChoices) {
                    str += listItems[mChoices.get(index)] + " ";
                }

                Toast.makeText(context,
                        "你选中了" + str,
                        Toast.LENGTH_SHORT).show();
            }
        });

        multiChoiceDialog.show();
    }


    /**
     * 进度条
     * @param context
     * @param ResId 标题图片id
     */
    public  static  void  showProgressDialog(Context context,int ResId) {
        final int MAX_PROGRESS = 100;
        final ProgressDialog progressDialog =
                new ProgressDialog(context);
        progressDialog.setProgress(0);
        progressDialog.setTitle("提示");
        progressDialog.setIcon(ResId);//fixme 可替换
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //..STYLE_SPINNER圆形 以上可根据需求进行替换
        progressDialog.setMax(MAX_PROGRESS);
        /* 模拟进度增加的过程
         * 新开一个线程，每个100ms，进度增加1
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                int progress= 0;
                while (progress < MAX_PROGRESS){
                    try {
                        Thread.sleep(100);
                        progress++;
                        progressDialog.setProgress(progress);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                // 进度达到最大值后，窗口消失
                progressDialog.cancel();
            }
        }).start();

    }

    /**
     * 编辑框dialog
     * @param ctx
     * @param ResId
     */
    public static void showInputDialog(Context ctx,int ResId) {
        final EditText editText = new EditText(ctx);
        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(ctx);
        inputDialog.setTitle("提示").setView(editText);
        inputDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(ctx,
                        editText.getText().toString(),
                        Toast.LENGTH_SHORT).show();
            }
        });
        inputDialog.show();
    }



    /**
     * 等待提示或进度条对话框
     */
    public void showDialog(Context context) {
        progDialog = new ProgressDialog(context);
        progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progDialog.setIndeterminate(false);
        progDialog.setCancelable(true);
        progDialog.setMessage("请稍后");
        progDialog.show();
    }


    /**
     * 隐藏进度条对话框
     */
    public void dismissDialog() {
        if (progDialog != null) {
            progDialog.dismiss();
        }
    }
}
